package com.cheer.ba.view.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cheer.ba.R;
import com.cheer.ba.listener.OnItemClickListener;
import com.cheer.ba.model.BankModel;
import com.cheer.ba.utils.StringUtils;
import com.cheer.lib_recycle_swipe.SwipeMenuAdapter;

import java.util.List;

/**
 * 我的银行卡
 * Created by Hq on 2017/7/13.
 */
public class MyBankAdp extends SwipeMenuAdapter<MyBankAdp.DefaultViewHolder> {

    //可滑动
    public static final int VIEW_TYPE_MENU = 1;
    //不可滑动
    public static final int VIEW_TYPE_NONE = 2;

    private List<BankModel> mBanks;

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public MyBankAdp(List<BankModel> banks) {
        this.mBanks = banks;
    }

    @Override
    public int getItemViewType(int position) {
        BankModel model = getItem(position);
        //true 不可以滑动 false 可以滑动
        boolean flag = model.flag;
        if (!flag) {
            return VIEW_TYPE_MENU;
        } else {
            return VIEW_TYPE_NONE;
        }
    }

    @Override
    public int getItemCount() {
        return mBanks == null ? 0 : mBanks.size();
    }

    public BankModel getItem(int position) {
        if (null != mBanks && !mBanks.isEmpty()) {
            return mBanks.get(position);
        }
        return new BankModel();
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.adp_item_bank, parent, false);
    }

    @Override
    public DefaultViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new DefaultViewHolder(realContentView);
    }

    @Override
    public void onBindViewHolder(DefaultViewHolder holder, int position) {
        BankModel model = getItem(position);
        if (null != model) {
            if (model.flag) {
                //添加银行卡
                holder.mLinearNoBank.setVisibility(View.VISIBLE);
                holder.mRelaHadBank.setVisibility(View.GONE);
                holder.mTxtNoBank.setVisibility(View.GONE);
            } else {
                //有银行卡
                holder.mLinearNoBank.setVisibility(View.GONE);
                holder.mRelaHadBank.setVisibility(View.VISIBLE);
                holder.mImgLogo.setVisibility(View.GONE);
                holder.mTxtBankName.setText("");
                holder.mTxtBankNum.setText(StringUtils.replaceBankString(model.bankNo, model.bankNo.length() - 4));
            }
            holder.setOnItemClickListener(mOnItemClickListener);
        }
    }

    static class DefaultViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        //有银行卡
        RelativeLayout mRelaHadBank;
        ImageView mImgLogo;
        TextView mTxtBankName;
        TextView mTxtBankNum;
        //没有银行卡
        LinearLayout mLinearNoBank;
        TextView mTxtNoBank;

        OnItemClickListener mOnItemClickListener;

        public DefaultViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            mRelaHadBank = (RelativeLayout) itemView.findViewById(R.id.id_my_default_card_rela);
            mImgLogo = (ImageView) itemView.findViewById(R.id.id_bank_logo_img);
            mTxtBankName = (TextView) itemView.findViewById(R.id.id_bank_name_txt);
            mTxtBankNum = (TextView) itemView.findViewById(R.id.id_bank_card_num_txt);

            mLinearNoBank = (LinearLayout) itemView.findViewById(R.id.id_no_bank_linear);
            mTxtNoBank = (TextView) itemView.findViewById(R.id.id_no_bank_txt);
        }

        public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
            this.mOnItemClickListener = onItemClickListener;
        }

        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(getAdapterPosition());
            }
        }
    }
}
